package br.inf.solutions.apresentacoes.ssp.view.composer.hierarchy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Textbox;

import br.com.vexillum.util.HibernateUtils;
import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.ModelNameController;
import br.inf.solutions.apresentacoes.ssp.control.PostGraduationController;
import br.inf.solutions.apresentacoes.ssp.control.SquareController;
import br.inf.solutions.apresentacoes.ssp.model.PostGraduation;
import br.inf.solutions.apresentacoes.ssp.model.Square;
import br.inf.solutions.apresentacoes.ssp.model.SquarePostGraduation;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyFunction;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyStructEntity;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.HierarchyStructType;
import br.inf.solutions.apresentacoes.ssp.model.hierarchy.ModelName;

@SuppressWarnings("serial")
@org.springframework.stereotype.Component
@Scope("prototype")
public class HierarchyStructFunctionComposer extends HierarchyStructFormComposer {

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		loadSquarePostComboboxes();
	}
	
	private Square searchSquare;
	
	public Square getSearchSquare() {
		return searchSquare;
	}

	public void setSearchSquare(Square searchSquare) {
		this.searchSquare = searchSquare;
	}

	@Override
	public HierarchyStructEntity getEntityObject() {
		HierarchyStructEntity struct = new HierarchyFunction();
		HierarchyStructType structType = (HierarchyStructType) arg.get("entityType");
		struct.setType(structType);
		return struct;
	}
	
	@SuppressWarnings("unchecked")
	public List<Square> getSquares(){
		SquareController controlador = SpringFactory.getController("squareController", SquareController.class, ReflectionUtils.prepareDataForPersistence(this));
		return (List<Square>) controlador.doAction("listAll").getList();
	}
	
	@SuppressWarnings("unchecked")
	public List<PostGraduation> getPosts(){
		if (searchSquare == null) {
			return new ArrayList<PostGraduation>();
		} else {
			((HierarchyFunction) getEntity()).setSquareRequired(searchSquare);
			List<PostGraduation> list = new ArrayList<>();
			for(SquarePostGraduation sqp : searchSquare.getSquarePostAssociations()){
				PostGraduation post = (PostGraduation) HibernateUtils.initialize(sqp.getPostGraduation());
				list.add(post);
			}
			HashMap<String, Object> data = new HashMap<>();
			data.put("listEntity", list);
			PostGraduationController controller = SpringFactory.getController("postGraduationController", PostGraduationController.class, data);
			
			return (List<PostGraduation>) controller.doAction("sortListByPattern").getList();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<ModelName> getFunctionsModelName(){
		ModelNameController controlador = SpringFactory.getController("modelNameController", ModelNameController.class, ReflectionUtils.prepareDataForPersistence(this));
		return (List<ModelName>) controlador.doAction("getFunctionsModelName").getList();
	}
	
	protected void loadSquarePostComboboxes() {
		Textbox tb;
		Combobox cb;

		tb = (Textbox) getComponentById(component, "fldSquareHidden");
		cb = (Combobox) getComponentById(component, "fldSquareRequired");

		if (tb != null) {
			cb.setValue(tb.getValue());
			setSearchSquare(((HierarchyFunction) getEntity()).getSquareRequired());
		}
		
		loadBinder();	

		tb = (Textbox) getComponentById(component, "fldPostHidden");
		cb = (Combobox) getComponentById(component, "fldPostRequired");

		if (tb != null) {
			cb.setValue(tb.getValue());
		}
		
	}
	
}
